﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataModels.Models;
using GenericTools.ReportProcessing;
using DataModels.Models.Context;

namespace GenericTools.Tests
{
    internal sealed class TestDataInitializer
    {
        public static void TruncateRelatedOptionBPlusTables()
        {
            using (var db = new IQSMS_TestDBContext())
            {
                var aa = from a in db.ANCForms select a;
                foreach (var a in aa) db.ANCForms.Remove(a);

                var bb = from m in db.ANCForm_Malaria select m;
                foreach (var b in bb) db.ANCForm_Malaria.Remove(b);

                var cc = from p in db.ANCForm_PMTCT select p;
                foreach (var c in cc) db.ANCForm_PMTCT.Remove(c);

                var dd = from v in db.ANCForm_VidokezoVyaHatari select v;
                foreach (var d in dd) db.ANCForm_VidokezoVyaHatari.Remove(d);

                var ee = from h in db.ANCForm_HudhurioLaKwanza select h;
                foreach (var e in ee) db.ANCForm_HudhurioLaKwanza.Remove(e);

                var ff = from m in db.ANCFormMetadatas select m;
                foreach (var f in ff) db.ANCFormMetadatas.Remove(f);

                db.SaveChanges();
            }
        }

        public static void RemoveReportIfExists(string facilityCode, string reportKeyword, int month, int year)
        {
            using (var db = new IQSMS_TestDBContext())
            {
                var receivedSMS = (from r in db.ReceivedSMS
                                   where r.FacCode == facilityCode &&
                                   r.Report.RepVersion == reportKeyword &&
                                   r.Period == month &&
                                   r.Year == year
                                   select r).FirstOrDefault();

                if (receivedSMS != null)
                {
                    db.ReceivedSMS.Remove(receivedSMS);
                    db.SaveChanges();
                }
            }
        }

        public static void TryToRegisterUser(string phoneNumber, string facilityCode)
        {
            string registrationText = string.Format("Automated Tester eng {0}", facilityCode);
            if (RegistrationInfo.IsUserRegistered(phoneNumber))
            {
                registrationText = string.Format("{0} RESUBMIT", registrationText);                             
                var register = new RegistrationInfo(phoneNumber, registrationText);
                register.Register();
            }
            else
            {
                var register = new RegistrationInfo(phoneNumber, registrationText);
                register.Register();
            }
        }
    }
}
